package com.tako;

import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Service {

    public static File desktop = new File(System.getProperty("user.home") + File.separator + "Desktop");

    public List<ListItem> addFolder() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        //设置标题
        directoryChooser.setTitle("选择文件夹");
        //设置打开初始地址
        directoryChooser.setInitialDirectory(desktop);
        File file = directoryChooser.showDialog(new Stage());
        return getItemList(new File[]{file});
    }

    public List<ListItem> batchAddFolder() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        //设置标题
        directoryChooser.setTitle("选择文件夹");
        //设置打开初始地址
        directoryChooser.setInitialDirectory(desktop);
        File file = directoryChooser.showDialog(new Stage());
        File[] files = file.listFiles(File::isDirectory);
        return getItemList(files);
    }

    private List<ListItem> getItemList(File[] files) {
        List<ListItem> mediaList = new ArrayList<>();
        if (files == null) {
            return new ArrayList<>();
        }
        for (File file : files) {
            ListItem media = new ListItem(file);
            mediaList.add(media);
        }
        return mediaList;
    }


}
